package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Employee;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品和对应的口味数据
     * @param dishDTO
     */
    @Transactional//涉及多表查询，有使用事务
    @Override
    public void saveWithFlavor(DishDTO dishDTO) {

        Dish dish = new Dish();
        //拷贝属性值
        BeanUtils.copyProperties(dishDTO,dish);
        //不需要设置创建时间修改时间等，直接加自动填充注解

        //向菜品表插入一条数据
        dishMapper.insert(dish);

        //获取insert语句生成的主键值,将这个值赋值给口味表，形成逻辑外键链接
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();

        //判断前端传递的口味数据是否存在
        if(flavors !=null && flavors.size() > 0){
            for (DishFlavor flavor : flavors) {
                //将这个值赋值给口味表，形成逻辑外键链接
                flavor.setDishId(dishId);
            }
            //向口味表插入多条数据
            dishFlavorMapper.insertBatch(flavors);
        }


    }

    /**
     * 菜品分页查询、根据菜品名称模糊查询、根据菜品分类查询、根据菜品状态查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult dishPageQuery(DishPageQueryDTO dishPageQueryDTO) {
        //开始分页查询，使用pagehelper插件实现,在SQL语句后面追加limit关键字，传递开始页码和每页数目
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());

        //实现根据
        Page<DishVO> dishlist = dishMapper.dishPageQuery(dishPageQueryDTO);

        long total = dishlist.getTotal();
        List<DishVO> records = dishlist.getResult();

        return new PageResult(total,records);
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
        //1.判断当前菜品是否起售状态
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if(dish.getStatus().equals(StatusConstant.ENABLE)){
                //起售中的菜品不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //2.判断当前菜品是否被套餐关联
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(setmealIds !=null && setmealIds.size()>0){
            //当前菜品被关联，不能删除
            log.info("当前选中的菜品于以下套餐关联：{}",setmealIds);
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //3.删除菜品数据
        log.info("删除菜品：{}",ids);
        dishMapper.deleteByIds(ids);

        //4.删除菜品关联的口味数据
        log.info("删除菜品口味数据：{}",ids);
        dishFlavorMapper.deleteByDishIds(ids);


    }

    /**
     * 根据菜品ID查询
     * @param dishId
     * @return
     */
    @Transactional
    @Override
    public DishVO getById(Long dishId) {
        DishVO dishVO = new DishVO();
        //根据ID查询菜品表
        Dish dish = dishMapper.getById(dishId);
        //根据ID查询菜品口味表
        List<DishFlavor> dishFlavor = dishFlavorMapper.getByDishId(dishId);

        //将查询的菜品表和菜品口味表封装
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavor);

        return dishVO;
    }

    /**
     * 更改菜品数据
     * @param dishDTO
     */
    @Transactional
    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        //修改菜品表基本信息,只需要菜品基本信息，不需要口味信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        //删除原有的口味数据
        dishFlavorMapper.deleteByDishId(dish.getId());
        //重新插入口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();

        //判断前端传递的口味数据是否存在
        if(flavors !=null && flavors.size() > 0){
            for (DishFlavor flavor : flavors) {
                //将这个值赋值给口味表，形成逻辑外键链接
                flavor.setDishId(dish.getId());
            }
            //向口味表插入多条数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 启售禁售菜品状态
     * @param status
     * @param id
     */
    @Override
    public void status(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);

        //调用update方法，只更新有变动数据
        dishMapper.update(dish);

    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    @Override
    public List<Dish> getByCategoryId(Long categoryId) {

        return dishMapper.getByCategoryId(categoryId);
    }


}
